/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "String.h"
#include <algorithm>

namespace UGame {
namespace Util {

    int snprintf(char *_Dest, int _Size, char *_Format, ...)
    {
        int _Rc = -1;
        std::va_list _Args;
        va_start(_Args, _Format);
        _Rc = Util::vsnprintf(_Dest, _Size, _Format, _Args);
        va_end(_Args);
        return _Rc;
    }

    Tokens StrSplit(const std::string& _Src, const std::string& _Sep)
    {
        Tokens _R;
        std::string _S;
        for (std::string::const_iterator _I = _Src.begin(); _I != _Src.end(); ++_I)
        {
            if (_Sep.find(*_I) != std::string::npos) {
                if (_S.length()) {
                    _R.push_back(_S);
                }
                _S = "";
            }
            else {
                _S += *_I;
            }
        }
        if (_S.length()) { _R.push_back(_S); }
        return _R;
    }

    void TrimString(std::string& _Str, const std::string &_Trim)
    {
        _Str.erase(0, _Str.find_first_not_of(_Trim));
        _Str.erase(_Str.find_last_not_of(_Trim) + 1);
    }

	void ToUpper(std::string& _Str)
	{
		std::transform(_Str.begin(), _Str.end(), _Str.begin(), toupper);
	}

	void ToLower(std::string& _Str)
	{
		std::transform(_Str.begin(), _Str.end(), _Str.begin(), tolower);
	}

}}